//
//  XLGiftViewController.m
//  XiangLove
//
//  Created by 宋振 on 2018/7/26.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLGiftViewController.h"
#import "XLPersonCenterTool.h"

#import "XLGiftViewCell.h"
#import "XLUserInfoDetailModel.h"


static NSString *const identifer = @"identifierGiftCell";

@interface XLGiftViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *  collectionView;

//数据

@end

@implementation XLGiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"礼物榜";
    
    [self showBackButton:NULL];
    
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        
        [self prefersStatusBarHidden];
        
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
        
    }
    
    //礼物列表请求
    [self requestSource];
    
    //设置页面
    [self setCustomeView];
}

- (void)requestSource
{
    [[[XLHTTPSessionManager sharedManager]XLV2GET:@"/api/v2/userInfo/GetUserGifts" parameters:@{@"userId":self.userId} resultClass:nil] subscribeNext:^(id x) {
        
        self.dataArray = x;
        
        [self.collectionView reloadData];
        
    } error:^(NSError *error) {
        
    }];
}

- (void)setCustomeView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.backgroundColor =[UIColor whiteColor];
    
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    //注册cell
    [self.collectionView registerClass:[XLGiftViewCell class] forCellWithReuseIdentifier:identifer];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XLGiftViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
    
    NSDictionary *dic = self.dataArray[indexPath.row];
    
    cell.dic = dic;
    
    
    return cell;
}

#pragma mark    -   -   - FlowLayout -  -   -
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScale(55), 97);
}

//横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return kScale(0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 34;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(21, kScale(20), 0, kScale(20));
}


#pragma mark    -   -   -懒加载-   -   -





- (BOOL)prefersStatusBarHidden{
    
    return NO;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
